package com.aqiang.day0528_shop.main.view.fragment

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.aqiang.day0528_shop.R
import com.aqiang.day0528_shop.main.adapter.CarAdapter
import com.aqiang.day0528_shop.main.entity.SearchEntity
import com.aqiang.day0528_shop.main.entity.SearchGoodsEntity
import com.aqiang.day0528_shop.main.entity.ShopTitle
import com.aqiang.day0528_shop.main.view.activity.OrderActivity
import com.aqiang.day0528_shop.sql.CarSql
import com.aqiang.day0528_shop.user.contract.UserContract
import com.aqiang.lib_core.common.SLog
import com.aqiang.lib_core.mvp.BaseFragment
import kotlinx.android.synthetic.main.fragment_car.*
import org.jetbrains.anko.startActivity

class CarFragment : BaseFragment<UserContract.LoginPresenter>() {
    private var carSql:CarSql? = null
    private var data:MutableList<ShopTitle>? = null
    private var data1:MutableList<ShopTitle>? = null
    private var carAdapter:CarAdapter? = null
    private var b:Boolean = false
    private var sum:Float = 0f
    override fun bindLayout(): Int {
        return R.layout.fragment_car
    }

    override fun createPresenter() {

    }

    override fun initView(savedInstanceState: Bundle?) {
        rv_frag_car.layoutManager = LinearLayoutManager(context)
    }

    override fun initData() {
        data = mutableListOf()
        data1 = mutableListOf()
        carSql = CarSql.getInstance(context!!)
        var array:Array<String> = arrayOf()
        carSql!!.use {
            val cursor = query(CarSql.nameDb, null, null, null, "sellerid", null, null)
            if(cursor != null){
                while (cursor.moveToNext()){
                    val shoptitle = cursor.getString(cursor.getColumnIndex("shoptitle"))
                    //val tilte = cursor.getString(cursor.getColumnIndex("tilte"))
                    //SLog.d("${tilte}")
                    data!!.add(ShopTitle(true,shoptitle))
                }
            }
            val cursor1 = query(CarSql.nameDb, null, null, null, null, null, null)
            if(cursor1 != null){
                while (cursor1.moveToNext()){
                    val shoptitle = cursor1.getString(cursor1.getColumnIndex("shoptitle"))
                    val tilte = cursor1.getString(cursor1.getColumnIndex("tilte"))
                    //val sellerid = cursor1.getLong(cursor1.getColumnIndex("sellerid"))
                    val price = cursor1.getFloat(cursor1.getColumnIndex("price"))
                    val img = cursor1.getString(cursor1.getColumnIndex("img"))
                   // SLog.d("${shoptitle}${tilte}${price}${img}")
                    data!!.add(ShopTitle(SearchEntity(shoptitle,tilte,price,img,false,1)))
                }
            }
        }
        //13245
        data1!!.add(data!!.get(0))
        data1!!.add(data!!.get(2))
        data1!!.add(data!!.get(1))
        data1!!.add(data!!.get(3))
        data1!!.add(data!!.get(4))
        if(carAdapter == null){
            //SLog.d("${data!!.get(1).title}")
            carAdapter = CarAdapter(R.layout.item_car_bottom,R.layout.item_car_top,data1!!)
            rv_frag_car.adapter = carAdapter
            carAdapter!!.setOnItemChildClickListener { adapter, view, position ->
                if(view.id == R.id.cb_item_shop){
                    if(b){
                        for (item in data1!!.indices){
                            data1?.get(item)?.inclu = false
                            if(data1!!.get(item).title.equals(data1!!.get(item+1).t.shoptitle)){
                                data1?.get(item + 1)?.t?.inclu = false
                                carAdapter!!.notifyDataSetChanged()
                                break
                            }
                        }
                        b = false
                    }else{
                        for (item in data1!!.indices){
                            data1?.get(item)?.inclu = true
                            if(data1!!.get(item).title.equals(data1!!.get(item+1).t.shoptitle)){
                                data1?.get(item + 1)?.t?.inclu = true
                                carAdapter!!.notifyDataSetChanged()
                                break
                            }
                        }
                        b = true
                    }

                }else if(view.id == R.id.tv_item_b_add){
                    data1?.get(position)?.t?.num = 1 +(data1?.get(position)?.t?.num!!)
                    val price = data1?.get(position)?.t?.price!!
                    data1?.get(position)?.t?.price = (data1?.get(position)?.t?.num!! * price)
//                    if(data1?.get(position)?.t?.inclu == true){
//                        sum = sum + price
//                        SLog.d("${sum}")
//                    }
//                    tv_frag_car_money.text = "合计:${sum}"
                    carAdapter!!.notifyDataSetChanged()
                }else if(view.id == R.id.tv_item_b_del){
                    if(data1?.get(position)?.t?.num!! <= 1){
                        data1?.get(position)?.t?.num = 1
                    }else{
                        data1?.get(position)?.t?.num = data1?.get(position)?.t?.num!! - 1
                    }
                    val price = data1?.get(position)?.t?.price!!
                    data1?.get(position)?.t?.price = (data1?.get(position)?.t?.num!! * price)
//                    if(data1?.get(position)?.t?.inclu == true){
//                        sum = sum + price
//                        SLog.d("${sum}")
//                    }
//                    tv_frag_car_money.text = "合计:${sum}"
                    carAdapter!!.notifyDataSetChanged()
                }
                //tv_frag_car_money.text = "合计:${sum}"
            }
        }else{
            carAdapter!!.notifyDataSetChanged()
        }


        cb_frag_car_all.setOnCheckedChangeListener { buttonView, isChecked ->
            for (item in data1!!.indices){
                if(isChecked){
                    data1?.get(item)?.inclu = isChecked
                    data1?.get(item)?.t?.inclu = isChecked
                    for (item in data1!!.indices){
                        if(data1?.get(item)?.t?.inclu == true){
                            sum += (data1?.get(item)?.t?.price)!! * (data1?.get(item)?.t?.num!!)
                            //SLog.d("${sum}")
                        }
                    }
                   // tv_frag_car_money.text = "合计:${sum}"
                    tv_frag_car_money.text = "合计:${sum}"
                }else{
                    data1?.get(item)?.inclu = isChecked
                    data1?.get(item)?.t?.inclu = isChecked
                    tv_frag_car_money.text = "合计:0.0"
                }
            }
            carAdapter?.notifyDataSetChanged()
        }

        tv_frag_car_del.setOnClickListener {
            data1?.clear()
            carAdapter?.notifyDataSetChanged()
            carSql!!.use {
                delete(CarSql.nameDb,null,null)
            }
        }
        bt_frag_car_jie.setOnClickListener {
            context?.startActivity<OrderActivity>(
                "money" to tv_frag_car_money.text.toString()
            )
        }
    }

    override fun initEvent() {

    }
}